//
//  SunCycleProgress.m
//  TBCycleProgress
//
//  Created by sunshine on 16/10/9.
//  Copyright © 2016年 SF. All rights reserved.
//

#import "SunCycleProgress.h"
#define degreesToRadians(x) (M_PI*(x)/180.0) //把角度转换成PI的方式
@interface SunCycleProgress()
{
    CAShapeLayer *_trackLayer;
    UIColor *_strokeColor;
    CAShapeLayer *_progressLayer;
    CAGradientLayer *_progressColor;
}
@end
@implementation SunCycleProgress

- (void)progressWithBGColor:(UIColor *)bgColor ProgressColor:(UIColor *)contentolor LineWidth:(float)lineWidth Start:(float)start End:(float)end Width:(float)progressWidth Height:(float)progressHeight{
    _trackLayer = [CAShapeLayer layer];//创建一个track shape layer
    _trackLayer.frame = CGRectMake(0, 0, progressWidth, progressHeight);
    [self.layer addSublayer:_trackLayer];
    _trackLayer.fillColor = [[UIColor clearColor] CGColor];//内部颜色
    _trackLayer.strokeColor = [bgColor CGColor];//指定path的渲染颜色
    _trackLayer.opacity = 0.6; //背景，透明度小一点
    _trackLayer.lineCap = kCALineCapRound;//指定线的边缘是圆的
    _trackLayer.lineWidth = lineWidth;//线的宽度
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(progressWidth / 2, progressHeight - progressWidth / 2 *sin(degreesToRadians(end)) - (progressWidth == progressHeight?0:lineWidth / 4)) radius:(progressWidth-lineWidth)/2 startAngle:degreesToRadians(start) endAngle:degreesToRadians(end) clockwise:YES];//用来构建圆形
    _trackLayer.path =[path CGPath]; //把path传递給layer，然后layer会处理相应的渲染，整个逻辑和CoreGraph是一致的。
    
    _progressLayer = [CAShapeLayer layer];
    _progressLayer.frame = CGRectMake(0, 0, progressWidth, progressHeight);
    _progressLayer.fillColor =  [[UIColor clearColor] CGColor];
    _progressLayer.strokeColor  = [[UIColor whiteColor] CGColor];
    _progressLayer.lineCap = kCALineCapRound;
    _progressLayer.lineWidth = lineWidth;
    _progressLayer.path = [path CGPath];
    _progressLayer.strokeEnd = 0;
    
    CALayer *gradientLayer = [CALayer layer];
    
    _progressColor =  [CAGradientLayer layer];
    _progressColor.frame = CGRectMake(0, 0, progressWidth, progressHeight);
    [_progressColor setColors:[NSArray arrayWithObjects:(id)[contentolor CGColor],(id)[contentolor CGColor], nil]];
    [gradientLayer addSublayer:_progressColor];

    
/**   渐变色  **/
/**
    CAGradientLayer *gradientLayer1 =  [CAGradientLayer layer];
    gradientLayer1.frame = CGRectMake(0, 0, self.frame.size.width/2, self.frame.size.height);
    [gradientLayer1 setColors:[NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor],(id)[[UIColor greenColor] CGColor], nil]];
    [gradientLayer1 setLocations:@[@0.5,@0.9,@1 ]];
    [gradientLayer1 setStartPoint:CGPointMake(0.5, 1)];
    [gradientLayer1 setEndPoint:CGPointMake(0.5, 0)];
    [gradientLayer addSublayer:gradientLayer1];
    
    CAGradientLayer *gradientLayer2 =  [CAGradientLayer layer];
    [gradientLayer2 setLocations:@[@0.1,@0.5,@1]];
    gradientLayer2.frame = CGRectMake(self.frame.size.width/2, 0, self.frame.size.width/2, self.frame.size.height);
    [gradientLayer2 setColors:[NSArray arrayWithObjects:(id)[[UIColor grayColor] CGColor],(id)[[UIColor blueColor] CGColor], nil]];
    [gradientLayer2 setStartPoint:CGPointMake(0.5, 0)];
    [gradientLayer2 setEndPoint:CGPointMake(0.5, 1)];
    [gradientLayer addSublayer:gradientLayer2];
**/
    
    
    
    
    [gradientLayer setMask:_progressLayer]; //用progressLayer来截取渐变层
    [self.layer addSublayer:gradientLayer];
}
-(void)setPercent:(float)percent duration:(float)duration changeColor:(UIColor *)color
{
    if (color) {
        [_progressColor setColors:[NSArray arrayWithObjects:(id)[color CGColor],(id)[color CGColor], nil]];
    }
    [CATransaction begin];
    [CATransaction setDisableActions:duration>0?NO:YES];
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
    [CATransaction setAnimationDuration:1];
    _progressLayer.strokeEnd = percent;
    [CATransaction commit];
}
@end
